package com.yanqu.road.server.manger.activity.flowercard;

import com.yanqu.road.dao.impl.activity.flowercard.FlowerCardConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.cat.Position;
import com.yanqu.road.entity.activity.flowercards.FlowerCard;
import com.yanqu.road.entity.activity.flowercards.FlowerCardBeautyNpc;
import com.yanqu.road.entity.activity.flowercards.FlowerCardFlowerSkill;
import com.yanqu.road.entity.activity.flowercards.GameStatus;
import com.yanqu.road.entity.activity.flowercards.config.FlowerCardSkillInfo;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.flowercard.FlowerCardsModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class FlowerCardsMgr extends TempMgr {

    static private FlowerCardsConfig config;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload Flower Card Activity start");

        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.FlowerCards.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no Flower Card Activity in show time");
            config = null;

        } else {

            ActivityInfo activityInfo = openActivityInfoList.get(0);
            FlowerCardsConfig tempCardsConfig = new FlowerCardsConfig(activityInfo);
            int tmpActivityId = activityInfo.getActivityId();

            FlowerCardConfigDaoImpl configDao = new FlowerCardConfigDaoImpl();
            //配置解析
            tempCardsConfig.setRoundList(configDao.getFlowerCardRoundInfoList(activityInfo.getActivityId()));
            tempCardsConfig.setScoreInfoList(configDao.getFlowerCardScoreInfoList(activityInfo.getActivityId()));
            tempCardsConfig.setSkillInfoList(configDao.getFlowerCardSkillInfoList(activityInfo.getActivityId()));

            //初始化配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);
            if(activityConfigMap != null){
                if (activityConfigMap.containsKey(eActivityConfigType.FLOWERCARD_SCORE_ITEM_ID.getName())) {
                    tempCardsConfig.setScoreItemId(activityConfigMap.get(eActivityConfigType.FLOWERCARD_SCORE_ITEM_ID.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.FLOWERCARD_ENERGY_ITEM_ID.getName())) {
                    tempCardsConfig.setEnergyItemId(activityConfigMap.get(eActivityConfigType.FLOWERCARD_ENERGY_ITEM_ID.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.FLOWERCARD_ENERGY_RESET_TIME.getName())) {
                    tempCardsConfig.setEnergyResetTime(activityConfigMap.get(eActivityConfigType.FLOWERCARD_ENERGY_RESET_TIME.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.FLOWERCARD_ENERGY_RESET_NUM.getName())) {
                    tempCardsConfig.setEnergyResetNum(activityConfigMap.get(eActivityConfigType.FLOWERCARD_ENERGY_RESET_NUM.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.FLOWERCARD_SKILL_RESET_CONSUME.getName())) {
                    tempCardsConfig.setSkillResetConsume(activityConfigMap.get(eActivityConfigType.FLOWERCARD_SKILL_RESET_CONSUME.getName()).getValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.FLOWERCARD_SCORE_BASE.getName())) {
                    tempCardsConfig.setScoreBase(activityConfigMap.get(eActivityConfigType.FLOWERCARD_SCORE_BASE.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.FLOWERCARD_SCORE_CONT_ADD.getName())) {
                    tempCardsConfig.setScoreContAdd(activityConfigMap.get(eActivityConfigType.FLOWERCARD_SCORE_CONT_ADD.getName()).getValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.FLOWERCARD_MARRIAGE_BASE.getName())) {
                    tempCardsConfig.setMarriageBase(activityConfigMap.get(eActivityConfigType.FLOWERCARD_MARRIAGE_BASE.getName()).getIntValue());
                }
                if (activityConfigMap.containsKey(eActivityConfigType.FLOWERCARD_SUFFERANCE_ADD.getName())) {
                    tempCardsConfig.setSufferanceAdd(StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.FLOWERCARD_SUFFERANCE_ADD.getName()).getValue(), "\\|"));
                }
                if (activityConfigMap.containsKey(eActivityConfigType.FLOWERCARD_SCORE_ORDER_ADD.getName())) {
                    tempCardsConfig.setScoreOrderAdd(StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.FLOWERCARD_SCORE_ORDER_ADD.getName()).getValue(), "\\|"));
                }
                if (activityConfigMap.containsKey(eActivityConfigType.FLOWERCARD_CHANGE_SCORE.getName())) {
                    tempCardsConfig.setChangeScore(activityConfigMap.get(eActivityConfigType.FLOWERCARD_CHANGE_SCORE.getName()).getIntValue());
                }
            }
            config = tempCardsConfig;
        }

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(FlowerCardsModule.class).activityReload();
        }

        getLogger().info("reload Flower Card Activity finish");
    }


    /**
     * 四个方向的元素
     */
    public static List<Integer> getFourDirection(int index, int rowNum, int colNum) {
        List<Integer> tempList = new ArrayList<>();
        int max = rowNum * colNum - 1;
        int min = 0;
        //上、左、下、右
        int[][] offset = {{0, 1},{-1, 0},{0, -1},{1, 0}};
        Position position = coordinate(index, rowNum, colNum);
        for (int[] ints : offset) {
            int x = position.getX() + ints[0];
            int y = position.getY() + ints[1];
            if(x < 0 || y < 0){
                continue;
            }
            if(x > colNum - 1 || y > rowNum - 1){
                continue;
            }
            int tempIndex = y * colNum + x;
            if(tempIndex < min && tempIndex > max){
                continue;
            }
            tempList.add(tempIndex);
        }
        //getLogger().info("=========== {} getFourDirection [{}]", index, StringUtils.listToString(tempList, ","));
        return tempList;
    }


    /**
     * 元素下标转成坐标位置
     */
    public static Position coordinate(int index, int rowNum, int colNum){
        int y = index / colNum;
        int x = index - y * colNum;
        return new Position(x, y);
    }

    public static FlowerCardsConfig getConfig() {
        return config;
    }

    public static int getActivityId(){
        if(config == null){
            return 0;
        }
        return config.getActivityInfo().getActivityId();
    }

    public static GameStatus copy(GameStatus src){
        GameStatus dest = new GameStatus();
        dest.setRound(src.getRound());

        for (FlowerCardFlowerSkill skill : src.getSkillList()) {
            dest.getSkillList().add(skill.copy());
        }

        dest.setEfface(src.getEfface());
        dest.setCombo(src.getCombo());
        dest.setFlowerDew(src.getFlowerDew());

        dest.getCardOrder().addAll(src.getCardOrder());

        dest.setSkillSelect(src.isSkillSelect());
        dest.setSettle(src.isSettle());

        dest.setRowNum(src.getRowNum());
        dest.setColNum(src.getColNum());

        for (FlowerCardBeautyNpc npc : src.getBeautyNpcList()) {
            dest.getBeautyNpcList().add(npc.copy());
        }

        dest.setTotalStep(src.getTotalStep());
        dest.setAffinity(src.getAffinity());
        dest.setRound(src.getRound());
        dest.setSelectTimes(src.getSelectTimes());

        dest.setStartTime(src.getStartTime());
        dest.setUseGoods(src.isUseGoods());

        return dest;
    }

    public static List<FlowerCard> copy(List<FlowerCard> srcList){
        List<FlowerCard> result = new ArrayList<>();
        for (FlowerCard card : srcList) {
            result.add(card.copy());
        }
        return result;
    }

    /**
     * 设置盘面的行列
     */
    public static void setRowCol(GameStatus gameStatus, int beautyNum){
        for (int i = 2; i < beautyNum; i++) {
            int j = beautyNum / i;
            if(i * j == beautyNum && (i + 1 == j || i == j)){
                gameStatus.setRowNum(i);
                gameStatus.setColNum(j);
                break;
            }
        }
    }

    public static int getSkillAddition(GameStatus tempStatus, int skillType, FlowerCardsConfig config) {
        int skillAddition = 0;
        FlowerCardFlowerSkill skill = getFlowerCardFlowerSkill(tempStatus, skillType);
        if(skill != null){
            skillAddition = getSkillAddition(config, skill.getType(), skill.getLv());
        }
        return skillAddition;
    }

    public static int getSkillAddition(FlowerCardsConfig config, int type, int lv) {
        int skillAddition = 0;
        FlowerCardSkillInfo skillInfo = config.getFlowerCardSkillInfo(type, lv);
        if(skillInfo != null){
            if(skillInfo.getSkillId() == 7){
                skillAddition = Integer.parseInt(skillInfo.getSkillParam());
            }else if(skillInfo.getSkillId() == 5){
                skillAddition = Integer.parseInt(skillInfo.getSkillParam());
            }else if(skillInfo.getSkillId() == 2){
                skillAddition = Integer.parseInt(skillInfo.getSkillParam());
            }else if(skillInfo.getSkillId() == 9){
                skillAddition = Integer.parseInt(skillInfo.getSkillParam());
            }else if(skillInfo.getSkillId() == 3){
                skillAddition = Integer.parseInt(skillInfo.getSkillParam());
            }
        }
        return skillAddition;
    }

    public static FlowerCardFlowerSkill getFlowerCardFlowerSkill(GameStatus tempStatus, int skillType) {
        for (FlowerCardFlowerSkill skill : tempStatus.getSkillList()) {
            if(skill.getType() == skillType){
                return skill;
            }
        }
        return null;
    }

    public static Property getRandomWeightReward(String rewardListStr, Random random) {
        Property reward = new Property();
        if(StringUtils.isNullOrEmpty(rewardListStr) || "0".equals(rewardListStr)){
            return reward;
        }
        List<String> rewardList = StringUtils.stringToStringList(rewardListStr, "\\|");
        for (String rewards : rewardList) {
            List<String> paramList = StringUtils.stringToStringList(rewards, ";");
            List<String> itemList = new ArrayList<>();
            List<Integer> weightList = new ArrayList<>();
            for (String param : paramList) {
                List<String> list = StringUtils.stringToStringList(param, ",");
                itemList.add(list.get(0));
                weightList.add(Integer.parseInt(list.get(1)));
            }
            int idx = RandomHelper.getRandomIndexByWeight(weightList, random);
            reward .addProperty(PropertyHelper.parseStringToProperty(itemList.get(idx)));
        }
        return reward;
    }

    public static ActivityInfo getActivityInfo(){
        if(config == null){
            return null;
        }
        return config.getActivityInfo();
    }
}
